From 7609792f0cbf8bf465b1dec1ef723fc221c65eb9 Mon Sep 17 00:00:00 2001 From: Mike Baker Date: Mon, 13 Feb 2006 13:13:40 +0000 Subject: [PATCH] massive rewrite of firstboot SVN-Revision: 3233 --- openwrt/package/base-files/Makefile | 1 + .../package/base-files/default/bin/firstboot | 124 ++++++++++-------- .../base-files/default/etc/init.d/S10boot | 12 +- .../base-files/default/etc/init.d/S45firewall | 1 - .../base-files/default/etc/init.d/S99done | 8 +- .../base-files/default/sbin/mount_root | 32 +++-- 6 files changed, 99 insertions(+), 79 deletions(-) diff --git a/openwrt/package/base-files/Makefile b/openwrt/package/base-files/Makefile index f291c4ec9a..69fa8e7597 100644 --- a/openwrt/package/base-files/Makefile +++ b/openwrt/package/base-files/Makefile @@ -47,6 +47,7 @@ $(IPKG_BASE): mkdir -p $(IDIR_BASE)/lib mkdir -p $(IDIR_BASE)/usr/lib mkdir -p $(IDIR_BASE)/usr/bin + mkdir -p $(IDIR_BASE)/mnt mkdir -p $(IDIR_BASE)/www ln -sf /tmp/resolv.conf $(IDIR_BASE)/etc/resolv.conf rm -rf $(IDIR_BASE)/var diff --git a/openwrt/package/base-files/default/bin/firstboot b/openwrt/package/base-files/default/bin/firstboot index 604721873a..ee082cffd2 100755 --- a/openwrt/package/base-files/default/bin/firstboot +++ b/openwrt/package/base-files/default/bin/firstboot @@ -1,65 +1,85 @@ #!/bin/sh # $Id$ -mount | grep squashfs >&- || { - echo "You do not have a squashfs partition; aborting" - echo "(firstboot cannot be run on jffs2 based firmwares)" - return -} - -[ -f "/tmp/.firstboot" ] && { - echo "firstboot is already running" - return -} -touch /tmp/.firstboot +rom=$(awk '/squashfs/ {print $2}' /proc/mounts) +jffs=$(awk '/jffs2/ {print $2}' /proc/mounts) -jdev=$(mount | awk '/jffs2/ {print $3}') +dupe() { # + cd $1 + echo -n "creating directories... " + { + cd $2 + find . -xdev -type d + echo "./dev ./jffs ./mnt ./proc ./tmp" + # xdev skips mounted directories + cd $1 + } | xargs mkdir -p + echo "done" -if [ -z "$jdev" ]; then - echo -n "Creating jffs2 partition... " - mtd erase OpenWrt >&- 2>&- - mount -t jffs2 /dev/mtdblock/4 /jffs + echo -n "setting up symlinks... " + for file in $(cd $2; find . -xdev -type f; find . -xdev -type l;) + do { + case "$file" in + "./rom/note") ;; #nothing + "./etc/config"|\ + "./etc/resolv.conf"|\ + "./usr/lib/ipkg/info") cp -af $2/$file $file;; + *) ln -sf /rom/${file#./*} $file;; + esac + } done echo "done" - cd /jffs -else - echo "firstboot has already been run" - echo "jffs2 partition is mounted, only resetting files" - cd $jdev -fi +} -exec 2>/dev/null +pivot() { # + pivot_root $1 $1$2 + mount none /proc -t proc + umount $2/proc + mount -o move $2/dev /dev + mount -o move $2/tmp /tmp +} -mount /dev/mtdblock/2 /rom -o ro +mountdp() { # + mount $1 $2 -t $4 + dupe $2 $rom + pivot $2 /rom +} -echo -n "creating directories... " -{ - cd /rom - find . -type d - cd - -} | xargs mkdir -echo "done" +# script run by mount_root +[ "$1" = "ramoverlay" ] && { + #1st stage + mountdp none /mnt -t tmpfs + exit 0 +} -echo -n "setting up symlinks... " -for file in $(cd /rom; find * -type f; find * -type l;) -do { - case "${file%/*}" in - "usr/lib/ipkg/info"|"etc/config") cp -f /rom/$file $file;; - *) ln -sf /rom/$file $file;; - esac -} done -echo "done" +[ "$1" = "switch2jffs" ] && { + #2nd stage + mtd erase OpenWrt + mount -o remount,ro none / # try to avoid fs changing while copying + mount -o bind / /mnt + mount /dev/mtdblock/4 /rom/jffs -t jffs2 + echo -n "copying files ... " + cp -a /mnt/* /rom/jffs + umount /mnt + echo "done" + pivot /rom /mnt + pivot /jffs /rom + jffs2root --clean + exit 0 +} -touch /tmp/resolv.conf -ln -s /tmp/resolv.conf /etc/resolv.conf +# script run manually +[ -z "$rom" ] && { + echo "You do not have a squashfs partition; aborting" + echo "(firstboot cannot be run on jffs2 based firmwares)" + exit 1 +} -umount /rom -mount none /jffs/proc -t proc -pivot_root /jffs /jffs/rom -mount none /dev -t devfs -mount none /tmp -t ramfs -umount /rom/proc -umount /rom/tmp -umount /rom/dev +[ \! -z "$jffs" ] && { + echo "firstboot has already been run" + echo "jffs2 partition is mounted, only resetting files" + dupe $jffs $rom + exit 0 +} -# normally created by boot script -mkdir -p /var/run +mtd erase OpenWrt +mountdp /dev/mtdblock/4 /jffs -t jffs2 diff --git a/openwrt/package/base-files/default/etc/init.d/S10boot b/openwrt/package/base-files/default/etc/init.d/S10boot index cb8be94b5c..9e7009d876 100755 --- a/openwrt/package/base-files/default/etc/init.d/S10boot +++ b/openwrt/package/base-files/default/etc/init.d/S10boot @@ -1,5 +1,5 @@ #!/bin/sh -echo "S" > /proc/jffs2_bbc +[ -f /proc/jffs2_bbc ] && echo "S" > /proc/jffs2_bbc mkdir -p /var/run mkdir -p /var/log @@ -11,9 +11,9 @@ sed 's/^[^#]/insmod &/' /etc/modules /etc/modules.d/* 2>&-|ash ifconfig lo 127.0.0.1 up ifconfig eth0 promisc -# configure the switch based on nvram if not in failsafe mode -[ -d /proc/switch/eth0 -a -z "$FAILSAFE" ] && { - for nr in `seq 0 15`; do +# configure the switch based on nvram +[ -d /proc/switch/eth0 ] && { + for nr in $(seq 0 15); do vp="$(nvram get vlan${nr}ports)" [ -z "$vp" -o -z "$(nvram get vlan${nr}hwname)" ] || { echo "$vp" > /proc/switch/eth0/vlan/$nr/ports @@ -27,7 +27,3 @@ echo ${HOSTNAME:=OpenWrt}>/proc/sys/kernel/hostname vconfig set_name_type VLAN_PLUS_VID_NO_PAD -# automagically run firstboot -[ -z "$FAILSAFE" -a -z "$(nvram get no_root_swap)" ] && { - { mount|grep "on / type jffs2" 1>&-; } || firstboot -} diff --git a/openwrt/package/base-files/default/etc/init.d/S45firewall b/openwrt/package/base-files/default/etc/init.d/S45firewall index 9069bf4a2f..96ac521334 100755 --- a/openwrt/package/base-files/default/etc/init.d/S45firewall +++ b/openwrt/package/base-files/default/etc/init.d/S45firewall @@ -1,7 +1,6 @@ #!/bin/sh ## Please make changes in /etc/firewall.user -${FAILSAFE:+exit} . /etc/functions.sh WAN=$(nvram get wan_ifname) diff --git a/openwrt/package/base-files/default/etc/init.d/S99done b/openwrt/package/base-files/default/etc/init.d/S99done index ce60337212..786a83f377 100755 --- a/openwrt/package/base-files/default/etc/init.d/S99done +++ b/openwrt/package/base-files/default/etc/init.d/S99done @@ -1,4 +1,10 @@ #!/bin/sh +sysctl -p >&- + +# automagically run firstboot +[ "$(nvram get no_root_swap)" != 1 ] && { + { mount|grep "on / type tmpfs" 1>&-; } && firstboot switch2jffs +} + # set leds to normal state echo "0x00" > /proc/sys/diag -sysctl -p >&- diff --git a/openwrt/package/base-files/default/sbin/mount_root b/openwrt/package/base-files/default/sbin/mount_root index 88a233662d..5f9ae37a87 100755 --- a/openwrt/package/base-files/default/sbin/mount_root +++ b/openwrt/package/base-files/default/sbin/mount_root @@ -1,37 +1,35 @@ #!/bin/sh -is_clean() { - OFFSET="$((0x$(dd if=/dev/mtdblock/1 bs=1 skip=$((0x14)) count=2 2>&- | hexdump | grep 0000000 | cut -d ' ' -f 2) - 1))" - dd if=/dev/mtdblock/1 bs=1 skip=$OFFSET count=1 2>&- | hexdump -v | grep ' 0000' > /dev/null && return 255 || return 0 +is_dirty() { + grep Broadcom /proc/cpuinfo >&- || return 1 + OFFSET="$(($(hexdump -v /dev/mtdblock/1 -s 20 -n 2 -e '"%d"')-1))" + return $(hexdump -v /dev/mtdblock/1 -s $OFFSET -n 1 -e '"%d"') } -if [ "$1" != "failsafe" ]; then +if [ "$1" != "failsafe" -a "$(nvram get no_root_swap)" != 1 ]; then + mtd unlock linux mount | grep jffs2 >&- if [ $? = 0 ] ; then if [ $(cat /proc/mtd | wc -l) = 6 ]; then - echo 5 > /proc/sys/diag - mtd unlock linux mtd erase OpenWrt jffs2root --move else - mtd unlock rootfs mount -o remount,rw /dev/root / fi else - if [ -z "$(nvram get no_root_swap)" ]; then - is_clean || { - mtd erase OpenWrt - mtd unlock linux - jffs2root --clean - } - mtd unlock OpenWrt - mount -t jffs2 /dev/mtdblock/4 /jffs + is_dirty + if [ $? = 0 ]; then + firstboot ramoverlay >&- 2>&- + else + mount /dev/mtdblock/4 /jffs pivot_root /jffs /jffs/rom mount none /proc -t proc - mount none /dev -t devfs - umount /rom/proc /rom/dev >&- + umount /rom/proc + mount -o move /rom/dev /dev fi fi fi + mount none /tmp -t tmpfs -o nosuid,nodev,mode=1777,size=50% mkdir -p /dev/pts mount none /dev/pts -t devpts +mount -t sysfs none /sys 2>&- -- 2.30.2